package com.hu.hutool.ThreadUtil;

import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 自定义线程池
 *
 * @Author CazCen
 * @Date 2021/11/22 21:07
 */
@Data
@Component(value = "ThreadPoolExecutorTestBeanName")

public class ThreadPoolExecutorTest implements InitializingBean {
    private static Logger logger = LoggerFactory.getLogger(ThreadPoolExecutorTest.class);


    private static ThreadPoolExecutorTest threadPoolExecutorTest = null;

    public static ThreadPoolExecutorTest getPoolConfig() {
        return threadPoolExecutorTest;
    }


    private ThreadPoolExecutor myThreadPool = null;


    @Override
    public void afterPropertiesSet() throws Exception {

        threadPoolExecutorTest = this;

        if (myThreadPool == null) {
            myThreadPool = new ThreadPoolExecutor(1, 2, 60L,
                    TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(10), new ThreadFactory() {
                @Override
                public Thread newThread(Runnable r) {
                    return new Thread(r, "my_thread_name_" + r.hashCode());
                }
            });
        }
    }
}
